package org.ljy.rabbitextensionsdemo.config;

import com.rabbitmq.client.AMQP;
import org.ljy.rabbitextensionsdemo.constant.Constant;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.batch.BatchDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AckConfig {

    //消息确认机制
    @Bean("ackQueue")
    public Queue ackQueue() {
        return QueueBuilder.durable(Constant.ACK_QUEUE).build();
    }

    @Bean("ackDirectExchange")
    public DirectExchange ackDirectExchange() {
        return ExchangeBuilder.directExchange(Constant.ACK_EXCHANGE).build();
    }

    @Bean("ackBinding")
    public Binding ackBinding(@Qualifier("ackDirectExchange") DirectExchange ackDirectExchange,
                              @Qualifier("ackQueue") Queue ackQueue) {
        return BindingBuilder.bind(ackQueue).to(ackDirectExchange).with("ack");
    }

    //持久化
    @Bean("preDirectExchange")
    public DirectExchange preDirectExchange() {
        return ExchangeBuilder.directExchange(Constant.PRE_EXCHANGE).durable(false).build();//飞持久化交换机
    }

    @Bean("preQueue")
    public Queue preQueue() {
        return QueueBuilder.nonDurable(Constant.PRE_QUEUE).build();//非持久化队列
    }

    @Bean
    public Binding preBinding(@Qualifier("preDirectExchange") Exchange preExchange,
                              @Qualifier("preQueue") Queue preQueue) {

        return BindingBuilder.bind(preQueue()).to(preDirectExchange()).with("pres");
    }


    //发送方确认机制
    @Bean("confirmQueue")
    public Queue confirmQueue() {
        return QueueBuilder.durable(Constant.CONFIRM_QUEUE).build();
    }
    @Bean("confirmDirectExchange")
    public DirectExchange confirmDirectExchange() {
        return ExchangeBuilder.directExchange(Constant.CONFIRM_EXCHANGE).build();
    }

    @Bean("confirmBinding")
    public Binding confirmBinding(@Qualifier("confirmDirectExchange")Exchange exchange,@Qualifier("confirmQueue")Queue queue ) {
        return BindingBuilder.bind(queue).to(exchange).with("confirm").noargs();
    }

    //重试机制
    @Bean("retryQueue")
    public Queue retryQueue() {
        return QueueBuilder.durable(Constant.RETRY_QUEUE).build();
    }
    @Bean("retryDirectExchange")
    public DirectExchange retryDirectExchange() {
        return ExchangeBuilder.directExchange(Constant.RETRY_EXCHANGE).build();
    }

    @Bean("retryBinding")
    public Binding retryBinding(@Qualifier("retryDirectExchange")Exchange exchange,@Qualifier("retryQueue")Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("retry").noargs();
    }

    //ttl
    @Bean("ttlQueue")
    public Queue ttlQueue() {
        return QueueBuilder.durable(Constant.TTL_QUEUE).build();
    }

    @Bean("ttlDirectExchange")
    public DirectExchange ttlDirectExchange() {
        return ExchangeBuilder.directExchange(Constant.TTL_EXCHANGE).build();
    }

    @Bean("ttlBinding")
    public Binding ttlBinding(@Qualifier("ttlQueue") Queue queue,@Qualifier("ttlDirectExchange") Exchange exchange ) {
        return BindingBuilder.bind(queue).to(exchange).with("ttl").noargs();
    }

    //队列ttl
    @Bean("ttlQueue2")
    public Queue ttlQueue2() {
        return QueueBuilder.durable(Constant.TTL_QUEUE2).ttl(20 * 1000).build();//队列中的消息20s后被删除
    }

    @Bean("ttlDirectExchange2")
    public DirectExchange ttlDirectExchange2() {
        return ExchangeBuilder.directExchange(Constant.TTL_EXCHANGE).build();
    }

    @Bean("ttlBinding2")
    public Binding ttlBinding2(@Qualifier("ttlQueue2") Queue queue,@Qualifier("ttlDirectExchange2") Exchange exchange ) {
        return BindingBuilder.bind(queue).to(exchange).with("ttl2").noargs();
    }




}
